#include "Sort.h"
#include <stdlib.h>

void shSwap(int* Arr, int i)
{
	int temp;
	temp = Arr[i];
	Arr[i] = Arr[i - 1];
	Arr[i - 1] = temp;
}

void ShakerSort(int* Arr, int Start, int N)
{
	int Left, Right;

	Left = Start;
	Right = N - 1;


	do
	{
		for (int i = Right; i >= Left; i--)
		{
			if (Arr[i - 1] > Arr[i])
			{
				shSwap(Arr, i);
			}
		}

		Left = Left + 1;

		for (int i = Left; i <= Right; i++)
		{
			if (Arr[i - 1] > Arr[i])
			{
				shSwap(Arr, i);
			}
		}

		Right = Right - 1;
	} while (Left <= Right);
}

void swap(int* a, int* b)
{
	int buf = *a;
	*a = *b;
	*b = buf;
}

void NaiveSort(int* arr, int length)
{
	int i, j;

	for (i = length - 1; i > 0; i--)
	{
		for (j = 0; j < i; j++)
		{
			if (arr[j] > arr[j + 1])
				swap(&arr[j], &arr[j + 1]);
		}
	}

}

void BubbleSort(int* arr, int length)
{
	int i, j, flag;

	for (i = length - 1; i > 0; i--)
	{
		flag = 0;
		for (j = 0; j < i; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				swap(&arr[j], &arr[j + 1]);
				flag = 1;
			}
		}
		if (!flag) return;
	}

}

int _cmp_int(const void *a, const void *b) {
	return *(int*)a - *(int*)b;
}

void qsort_common(int* arr, int length)
{
	qsort(arr, length, sizeof(int), _cmp_int);
}

void ShakerSort_common(int* arr, int length)
{
	ShakerSort(arr,0, length);
}